//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ruoyi.system.controller;


import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageDomain;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.domain.vo.UserVo;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping({"/role"})
public class SysRoleController extends BaseController {
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysUserService userService;

    public SysRoleController() {
    }

    @RequiresPermissions({"system:role:list"})
    @PostMapping({"/list"})
    public TableDataInfo list(@RequestBody SysRole role) {
        PageDomain pageDomain = new PageDomain();
        BeanUtils.copyProperties(role, pageDomain);
        this.startPage(pageDomain);
        List<SysRole> list = this.roleService.selectRoleList(role);
        return this.getDataTable(list);
    }

    @Log(
            title = "角色管理",
            businessType = BusinessType.EXPORT
    )
    @RequiresPermissions({"system:role:export"})
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, SysRole role) {
        List<SysRole> list = this.roleService.selectRoleList(role);
        ExcelUtil<SysRole> util = new ExcelUtil(SysRole.class);
        util.exportExcel(response, list, "角色数据");
    }

    @RequiresPermissions({"system:role:query"})
    @GetMapping({"getInfo"})
    public AjaxResult getInfo(Long roleId) {
        if (roleId != null && roleId != 0L) {
            this.roleService.checkRoleDataScope(roleId);
            return AjaxResult.success(this.roleService.selectRoleById(roleId));
        } else {
            return AjaxResult.error(-1, "角色id不能为空");
        }
    }

    @RequiresPermissions({"system:role:add"})
    @Log(
            title = "角色管理",
            businessType = BusinessType.INSERT
    )
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysRole role) {
        if ("1".equals(this.roleService.checkRoleNameUnique(role))) {
            return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if ("1".equals(this.roleService.checkRoleKeyUnique(role))) {
            return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败，角色权限已存在");
        } else {
            role.setCreateBy(SecurityUtils.getUsername());
            return this.toAjax(this.roleService.insertRole(role));
        }
    }

    @RequiresPermissions({"system:role:edit"})
    @Log(
            title = "角色管理",
            businessType = BusinessType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@RequestBody SysRole role) {
        this.roleService.checkRoleAllowed(role);
        this.roleService.checkRoleDataScope(role.getRoleId());
        if ("1".equals(this.roleService.checkRoleNameUnique(role))) {
            return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if ("1".equals(this.roleService.checkRoleKeyUnique(role))) {
            return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
        } else {
            role.setUpdateBy(SecurityUtils.getUsername());
            return this.toAjax(this.roleService.updateRole(role));
        }
    }

    @RequiresPermissions({"system:role:edit"})
    @Log(
            title = "角色管理",
            businessType = BusinessType.UPDATE
    )
    @PostMapping({"/dataScope"})
    public AjaxResult dataScope(@RequestBody SysRole role) {
        this.roleService.checkRoleAllowed(role);
        this.roleService.checkRoleDataScope(role.getRoleId());
        return this.toAjax(this.roleService.authDataScope(role));
    }

    @RequiresPermissions({"system:role:edit"})
    @Log(
            title = "角色管理",
            businessType = BusinessType.UPDATE
    )
    @PostMapping({"/changeStatus"})
    public AjaxResult changeStatus(@RequestBody SysRole role) {
        this.roleService.checkRoleAllowed(role);
        this.roleService.checkRoleDataScope(role.getRoleId());
        role.setUpdateBy(SecurityUtils.getUsername());
        return this.toAjax(this.roleService.updateRoleStatus(role));
    }

    @RequiresPermissions({"system:role:remove"})
    @Log(
            title = "角色管理",
            businessType = BusinessType.DELETE
    )
    @PostMapping({"/remove"})
    public AjaxResult remove(@RequestBody Long[] roleIds) {
        return this.toAjax(this.roleService.deleteRoleByIds(roleIds));
    }

    @RequiresPermissions({"system:role:query"})
    @GetMapping({"/optionselect"})
    public AjaxResult optionselect() {
        return AjaxResult.success(this.roleService.selectRoleAll());
    }

    @RequiresPermissions({"system:role:list"})
    @GetMapping({"/authUser/allocatedList"})
    public TableDataInfo allocatedList(SysUser user) {
        this.startPage();
        List<SysUser> list = this.userService.selectAllocatedList(user);
        return this.getDataTable(list);
    }

    @RequiresPermissions({"system:role:list"})
    @GetMapping({"/authUser/unallocatedList"})
    public TableDataInfo unallocatedList(SysUser user) {
        this.startPage();
        List<SysUser> list = this.userService.selectUnallocatedList(user);
        return this.getDataTable(list);
    }

    @RequiresPermissions({"system:role:edit"})
    @Log(
            title = "角色管理",
            businessType = BusinessType.GRANT
    )
    @PostMapping({"/authUser/cancel"})
    public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole) {
        return this.toAjax(this.roleService.deleteAuthUser(userRole));
    }

    @RequiresPermissions({"system:role:edit"})
    @Log(
            title = "角色管理",
            businessType = BusinessType.GRANT
    )
    @PostMapping({"/authUser/cancelAll"})
    public AjaxResult cancelAuthUserAll(@RequestBody UserVo userVo) {
        return this.toAjax(this.roleService.deleteAuthUsers(userVo.getRoleId(), userVo.getUserIds()));
    }

    @RequiresPermissions({"system:role:edit"})
    @Log(
            title = "角色管理",
            businessType = BusinessType.GRANT
    )
    @PostMapping({"/authUser/selectAll"})
    public AjaxResult selectAuthUserAll(@RequestBody UserVo userVo) {
        this.roleService.checkRoleDataScope(userVo.getRoleId());
        return this.toAjax(this.roleService.insertAuthUsers(userVo.getRoleId(), userVo.getUserIds()));
    }
}
